/*
	新增物流节点
*/

//登录信息
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

//dom
$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer, form = layui.form;

		//ajaxData
		var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };

		//新增函数
		function addNewFreightNode(callData){
			/*
				新增物流节点 
				因接口问题 已存在会进行修改
			*/
			var _ajaxData = $.extend(ajaxData, callData);
			//console.log(_ajaxData)
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });

			$.ajax({
				url: BASE_SERVICEURL + URlS.freight,
				type: 'PUT',
				data: _ajaxData,
				success: function(responseData){
					//console.log(responseData)
					var code = responseData.msg.code;
					if(code == 200){
						layer.close(loadIndex);
						layer.msg('新增或修改成功', {time: 2000, end: function(){
							//关闭当前窗口并刷新父窗口
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else if(code == 2002){
						layer.msg('接口缺少必要参数，请确认数据均以填写后重试！', {time: 2000});
						layer.close(loadIndex)
					}else if(code == 2005){
						//登录失效
						layer.msg('登录失效，请重新登陆后重试', {time: 2000, end: function(){
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error , { time: 2000, end: function(){
                    	window.location.href = './500.html';
                    }});
				}
			});
		}
		//提交新增
		form.on('submit(add)', function(data){
			//console.log(data);
			layer.confirm('确认以上述信息新增物流节点？', {title:'提示'}, function(index){
				//组织数据
				var f = data.field;
				var callData = {
					'name': f.name,
					'text': f.text,
					'abbr': f.nodeID,
					'freight': langTransObj(f.freightCn, f.freightTw, f.freightEn),
					'free': langTransObj(f.standardCn, f.standardTw, f.standardEn),
					'status': f.status
				};
				//请求新增
				addNewFreightNode(callData)
				//关闭询问框
				layer.close(index); 
			});
			return false;
		});

	});
})




